package com.information.controller;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.infoland.jfinal.BaseController;
import com.information.service.AdministratorService;
import com.information.service.ClassService;
import com.information.service.GradeService;
import com.information.service.StudentService;
import com.information.service.TeacherService;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;


public class Controller extends BaseController{
	/**
	 * 登陆界面
	 */
	public void index() {
		render("/login1.html");
	}
	/**
	 * 判断验证码和用户名,密码
	 */
	public void loginValidation() {
		if (!validateCaptcha("code")) {
			Ret ret=new Ret();
			ret.setFail();
			ret.set("message","验证码错误");
			renderJson(ret);
		}else {
		String username=getPara("username");
		String password=getPara("password");
		Ret verify=AdministratorService.adminService.verifyLanding(username, password);
		renderJson(verify);
		}
	}
	/**
	 * 验证码
	 */
	public void vCode() {
		renderCaptcha();
	}
	/**
	 * 首页
	 */
	public void home() {
		render("/index.html");
	}
	/**
	 * 首页数据
	 */
	public void homePage() {
		int pageNum=getJsonParaToInt("pageNum",1);
		Page<Record> students=StudentService.service.students(pageNum);
		//遍历学生对象数组
		for(Record s:students.getList()) {
			//没有创建时期的把日期改为提示
			if (s.getStr("addDate")==null) {
				//把修改完的放到对象中
				s.set("addDate", "无法获取创建记录");
			}
			//没有修改时期的把日期改为提示
			if (s.getStr("update")==null) {
				//把修改完的放到对象中
				s.set("update", "暂无修改记录");
			}
		}
		renderJson(students);
	}
	/**
	 * 查询单个Id
	 */
	public void theQuery() {
		int id=getJsonParaToInt("student_id");
		Record record=StudentService.service.student(id);
		renderJson(record);
	}
	/**
	 * 获取班级信息
	 */
	public void classInformation() {
		List<Record> classList=ClassService.classService.Class();
		renderJson(classList);
	}
	/**
	 * 获取年级信息
	 */
	public void gradeInformation() {
		List<Record> gradeList=GradeService.gradeService.Grade();
		renderJson(gradeList);
	}
	/**
	 * 获取班主任信息
	 */
	public void teacherInformation() {
		List<Record> guideMemberList=TeacherService.teacherService.Teacher();
		renderJson(guideMemberList);
	}
	/**
	 * 修改
	 */
	public void updata() {
		Record record=getJsonRecord("student");
		Ret ret=new Ret();
		if (StudentService.service.updata(record)) {
			ret.setOk();
			renderJson(ret);
		}
	}
	/**
	 * 删除
	 */
	public void delect() {
		int id=getJsonParaToInt("id");
		Ret ret=new Ret();
		if (StudentService.service.delect(id)) {
			ret.setOk();
			renderJson(ret);
		}
	}
	/**
	 * 删除选中
	 */
	public void delectList() {
		JSONArray idlist=getJsonArray("idlist");
		Ret ret=new Ret();
		if (StudentService.service.delect(idlist)) {
			ret.setOk();
			renderJson(ret);
		}
	}
	/**
	 * 新增
	 */
	public void save() {
		Record record=getJsonRecord("student");
		Ret ret=new Ret();
		if (StudentService.service.save(record)) {
			ret.setOk();
			renderJson(ret);
		}
	}
}
